package com.yigo.toc.infra.repo;

import com.yigo.framework.http.BException;
import com.yigo.framework.http.RCodeEnum;
import com.yigo.toc.domain.to.aggregateroot.TradeOrder;
import com.yigo.toc.domain.to.repo.TradeOrderRepo;
import com.yigo.toc.infra.repo.converter.TcDmo2PoConverter;
import com.yigo.toc.infra.repo.dataobject.TocOrderAddressPO;
import com.yigo.toc.infra.repo.dataobject.TocOrderHeadPO;
import com.yigo.toc.infra.repo.dataobject.TocOrderLinePO;
import com.yigo.toc.infra.repo.mapper.TocOrderAddressMapper;
import com.yigo.toc.infra.repo.mapper.TocOrderHeadMapper;
import com.yigo.toc.infra.repo.mapper.TocOrderLineMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author chenzl
 */
@Repository
public class TradeOrderRepoImpl implements TradeOrderRepo {
    @Resource
    private TocOrderHeadMapper tocOrderHeadMapper;
    @Resource
    private TocOrderLineMapper tocOrderLineMapper;
    @Resource
    private TocOrderAddressMapper tocOrderAddressMapper;

    @Override
    public TradeOrder save(TradeOrder tradeOrder) {
        TocOrderHeadPO headPO = TcDmo2PoConverter.INSTANCE.toPO(tradeOrder);
        if (null == headPO.getId()) {
            if (tocOrderHeadMapper.insert(headPO) == 0) {
                throw new BException(RCodeEnum.PERSIST_OBJECT_ERROR);
            }
            tradeOrder.setId(headPO.getId());
            Optional.ofNullable(tradeOrder.getDetails()).ifPresent(list -> list.forEach(x -> {
                x.setHeadId(tradeOrder.getId());
                TocOrderLinePO linePO = TcDmo2PoConverter.INSTANCE.toPO(x);
                if (tocOrderLineMapper.insert(linePO) == 0) {
                    throw new BException(RCodeEnum.PERSIST_OBJECT_ERROR);
                }
                x.setId(linePO.getId());
            }));
            Optional.ofNullable(tradeOrder.getAddress()).ifPresent(x -> {
                x.setHeadId(tradeOrder.getId());
                TocOrderAddressPO addressPO = TcDmo2PoConverter.INSTANCE.toPO(x);
                if (tocOrderAddressMapper.insert(addressPO) == 0) {
                    throw new BException(RCodeEnum.PERSIST_OBJECT_ERROR);
                }
                x.setId(addressPO.getId());
            });
        } else {
            if (tocOrderHeadMapper.updateById(headPO) == 0) {
                throw new BException(RCodeEnum.PERSIST_OBJECT_ERROR);
            }
            Optional.ofNullable(tradeOrder.getDetails()).ifPresent(list -> list.forEach(x -> {
                TocOrderLinePO linePO = TcDmo2PoConverter.INSTANCE.toPO(x);
                if (tocOrderLineMapper.updateById(linePO) == 0) {
                    throw new BException(RCodeEnum.PERSIST_OBJECT_ERROR);
                }
            }));
            Optional.ofNullable(tradeOrder.getAddress()).ifPresent(x -> {
                TocOrderAddressPO addressPO = TcDmo2PoConverter.INSTANCE.toPO(x);
                if (tocOrderAddressMapper.updateById(addressPO) == 0) {
                    throw new BException(RCodeEnum.PERSIST_OBJECT_ERROR);
                }
            });
        }
        return tradeOrder;
    }
}
